package com.example.demo.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.example.demo.entity.PaneProperty;

/**
 * @title:	DrawPictureUtil
 * @author:	许子文
 * @date:	2019年2月16日 
 * @descirption: 画图工具
 */
public class DrawPictureUtil {
	private static Logger logger = LoggerFactory.getLogger(DrawPictureUtil.class);
	
	/**
	 * @title:	drawPane
	 * @author: 许子文
	 * @date: 2019年2月16日
	 * @description: 绘制单个矩形框
	 */
	public static void drawPane(String oldImgPath,String newImgPath,String formatName,int x,int y,int width,int height) throws Exception{
		BufferedImage bi = ImageIO.read(new File(oldImgPath));
		
		Graphics2D g = (Graphics2D)bi.getGraphics();
		
		//g.setColor(new Color(60, 173, 230));
		g.setColor(Color.GREEN);
		g.setStroke(new BasicStroke(2.0f));
		
		g.drawRect(x, y, width, height);
		
		FileOutputStream out = new FileOutputStream(newImgPath);
		
		ImageIO.write(bi, formatName.substring(1), out);
		
		out.close();
		
		bi = null;
		
		logger.debug("绘制完成");
	}
	
	/**
	 * @title:	drawPoint
	 * @author: 许子文
	 * @date: 2019年2月16日
	 * @description: 绘制一个点
	 */
	public static void drawPoint(String oldImgPath,String newImgPath,String formatName,int x,int y) throws Exception{
		BufferedImage bi = ImageIO.read(new File(oldImgPath));
		
		Graphics2D g = (Graphics2D)bi.getGraphics();
		
		g.setColor(Color.red);
		
		int width = 16;
		
		int height = 16;
		
		x = x-8;
		
		y = y-8;
		
		g.fillOval(x, y, width, height);
		
		FileOutputStream out = new FileOutputStream(newImgPath);
		
		ImageIO.write(bi, formatName.substring(1), out);
		
		out.close();
		
		logger.debug("绘制完成");
	}
	
	/**
	 * @title:	drawline
	 * @author: 许子文
	 * @date: 2019年2月16日
	 * @description: 绘制一条直线
	 */
	public static void drawline(String oldImgPath,String newImgPath,String formatName,int startx,int starty,int endx,int endy) throws Exception{
		BufferedImage bi = ImageIO.read(new File(oldImgPath));
		
		Graphics2D g = (Graphics2D)bi.getGraphics();
		
		//g.setColor(new Color(60, 173, 230));
		g.setColor(Color.GREEN);
		g.setStroke(new BasicStroke(2f));
		
		g.drawLine(startx, starty, endx, endy);
		
		FileOutputStream out = new FileOutputStream(newImgPath);
		
		ImageIO.write(bi, formatName.substring(1), out);
		
		out.close();
		
		logger.debug("绘制完成");
	}
	
	/**
	 * @title writeText
	 * @author: 许子文
	 * @date: 2019年3月18日
	 * @description: 给图片添加文字
	 */
	public static void writeText(String oldImgPath,String newImgPath,String formatName,String text,int x,int y) throws Exception{
		BufferedImage bi = ImageIO.read(new File(oldImgPath));
		
		Graphics2D g = (Graphics2D) bi.getGraphics();
		
		g.setColor(Color.GREEN);
		g.setFont(new Font("微软雅黑", Font.PLAIN, 35));
		
		g.drawString(text, x, y);
		
		FileOutputStream out = new FileOutputStream(newImgPath);
		
		ImageIO.write(bi, formatName.substring(1), out);
		
		out.close();
		
		logger.debug("标记文字完成");
		
	}
	
	/**
	 * @title:	drawPanes
	 * @author:	许子文
	 * @date:	2018年12月26日
	 * @description: 在图片上绘制方框绘制方框
	 * @param oldImgPath原图片路径
	 * @param newImgPath绘制以后的图片路径
	 * @param formatName图片格式(格式:".jpg")
	 * @param paneList缺陷点集合
	 */
	public static void drawPanes(String oldImgPath,String newImgPath,String formatName,List<PaneProperty> paneList) throws Exception{
		BufferedImage bi = ImageIO.read(new File(oldImgPath));
		
		Graphics2D g = (Graphics2D)bi.getGraphics();
		
		g.setColor(Color.red);
		g.setStroke(new BasicStroke(10.0f));
		
		//如果没有缺陷点则会复制原图
		if(EmptyUtil.isNotEmpty(paneList)){
			int len = paneList.size();
			
			for (int i = 0; i < len; i++) {
				int x = Integer.parseInt(paneList.get(i).getX_coord());
				int y = Integer.parseInt(paneList.get(i).getY_coord());
				int width = Integer.parseInt(paneList.get(i).getWidth());
				int height = Integer.parseInt(paneList.get(i).getHeight());
				
				g.drawRect(x, y, width, height);
			}
		}
		
		FileOutputStream out = new FileOutputStream(newImgPath);
		
		ImageIO.write(bi, formatName.substring(1), out);
		
		out.close();
		
		bi = null;
		
		logger.debug("绘制完成");
		
	}
}
